<template>
  <div>
    <div
      style="padding-top: 30px"
      v-if="showSearch"
      :class="{
        'enter-level flipInX': showSearch,
      }"
    >
      <el-form :inline="true" :model="searchFrom" class="demo-form-inline">
        <el-form-item label="公告标题">
          <el-input
            v-model="searchFrom.title"
            placeholder="请输入公告标题"
            clearable
          />
        </el-form-item>
        <el-form-item label="操作人员">
          <el-input
            v-model="searchFrom.username"
            placeholder="请输入操作人员"
            clearable
          />
        </el-form-item>

        <el-form-item label="类型">
          <el-select
            v-model="searchFrom.type"
            placeholder="请选择公告类型"
            clearable
          >
            <el-option label="通知" value="1" />
            <el-option label="公告" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :icon="Search">
            搜索
          </el-button>
          <el-button type="info" @click="reset" :icon="Refresh">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="padding: 30px">
      <div
        style="
          padding-bottom: 20px;
          display: flex;
          justify-content: space-between;
        "
      >
        <div>
          <el-button :icon="Plus" type="success" @click="insertOne"
            >新增</el-button
          >
          <el-popconfirm
            title="确定删除吗？"
            @confirm="batchDel"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" :disabled="isBatch">
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </div>
        <div>
          <el-button
            :icon="Search"
            circle
            @click="showSearch = !showSearch"
          ></el-button>
          <el-button :icon="Refresh" circle @click="reset"></el-button>
        </div>
      </div>
      <el-table
        ref="multipleTableRef"
        :data="dataList"
        style="width: 100%"
        height="600"
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" :selectable="selectable" width="55" />
        <el-table-column
          type="index"
          :index="indexMethod"
          label="编号"
          width="105"
          fixed
        />
        <el-table-column property="title" label="公告标题" width="200" />
        <el-table-column
          property="content"
          label="公告内容"
          width="500"
          show-overflow-tooltip
        />
        <el-table-column label="公告类型" width="130">
          <template #default="scope">
            <el-tag
              :type="scope.row.type === 0 ? 'warning' : 'success'"
              disable-transitions
              >{{ scope.row.type === 0 ? "公告" : "通知" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="状态" width="130">
          <template #default="scope">
            <el-tag
              :type="scope.row.status === 0 ? 'info' : 'error'"
              disable-transitions
              >{{ scope.row.status === 0 ? "正常" : "关闭" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column property="operUser" label="创建者" width="130" />
        <el-table-column label="创建时间" width="200">
          <template #default="scope">{{
            tool.formatDateToYYYYMMDD(scope.row.createTime)
          }}</template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="300">
          <template #default="scope">
            <el-button @click="oper(scope.row, 1)" type="text" :icon="EditPen">
              修改
            </el-button>
            <el-popconfirm
              title="确定删除吗？"
              @confirm="oper(scope.row.id, 2)"
              @cancel="cancelEvent"
            >
              <template #reference>
                <el-button type="text" :icon="Delete"> 删除 </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 10px">
        <el-pagination
          :hide-on-single-page="true"
          background
          layout="prev, pager, next"
          :total="page.total"
          :page-size="page.size"
          :page-count="page.pages"
          @current-change="changePage"
          :pager-count="15"
        />
      </div>
    </div>
  </div>
  <add-notice ref="addDataRef" @common="handlerCommon"></add-notice>
</template>

<script setup>
import request from "@/assets/js/request";
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import addNotice from "@/components/setting/add-notice.vue";
import tool from "@/assets/js/tool";
import {
  Search,
  Refresh,
  EditPen,
  Delete,
  Plus,
} from "@element-plus/icons-vue";

const showSearch = ref(true);
const searchFrom = ref({
  title: "",
  type: null, // 默认全部
  username: "",
  pageSize: 20,
  pageNum: 1,
});
const loading = ref(false);
const addDataRef = ref();
const selectable = ref();
const dataList = ref([]);
const isBatch = ref(true);
const selectData = ref([]);
const page = ref({
  size: 20,
  num: 1,
  pages: 1,
  total: 0,
});

const indexMethod = (index) => {
  return index + 1;
};
//获取数据
const getDataList = () => {
  loading.value = true;
  searchFrom.value.pageSize = page.value.size;
  searchFrom.value.pageNum = page.value.num;
  request
    .post(`/notice/list`, searchFrom.value)
    .then((res) => {
      if (res.code === 200) {
        dataList.value = res.data.records;
        page.value.num = res.data.current;
        page.value.pages = res.data.pages;
        page.value.total = res.data.total;
      } else {
        ElMessage.error(res.msg);
      }
      loading.value = false;
    })
    .catch((err) => {
      ElMessage.error(err.message);
    });

  setTimeout(() => {
    loading.value = false;
  }, 5000);
};

const changePage = (pageIndex) => {
  console.log(pageIndex);
  page.value.num = pageIndex;
  getDataList();
};

const cancelEvent = () => {
  selectData.value = [];
};

// 新增数据
const insertOne = () => {
  addDataRef.value.open(null, "添加公告");
};

// 数据操作
const oper = (key, type) => {
  if (type == 1) {
    addDataRef.value.open(key, "修改公告");
    return;
  } else {
    selectData.value = [key];
    batchDel();
  }
};

const handlerCommon = (item) => {
  request
    .post(`/notice/insert`, item)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("操作成功！");
        getDataList();
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch((err) => {
      ElMessage.error(err.message);
    });
};

// 批量删除
const batchDel = () => {
  console.log(Object.values(selectData.value));
  request
    .post("/notice/del", Object.values(selectData.value))
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        getDataList();
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch((err) => {
      ElMessage.error(err.message);
    });
};

const onSubmit = () => {
  if (
    searchFrom.value.key == "" &&
    searchFrom.value.note == "" &&
    searchFrom.value.status == null &&
    searchFrom.value.type === null
  ) {
    return;
  }
  getDataList();
};
const reset = () => {
  searchFrom.value = {
    title: "",
    type: null, // 默认全部
    user: "",
  };
  getDataList();
};

// 多选
const handleSelectionChange = (val) => {
  let arr = val.map((item) => item.id);

  selectData.value = arr;
  console.log(selectData.value);
  if (selectData.value.length > 0) {
    isBatch.value = false;
  } else {
    isBatch.value = true;
  }
};

onMounted(() => {
  getDataList();
});
</script>

<style scoped lang="scss">
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.el-form-item__label {
  font-weight: bold;
}

.el-form {
  display: inline;
}
</style>
